define([ './module' ], function(module) {
	'use strict';


	module.controller('promotionproductseckillcontent',serverpromotionproductseckillcontentctrl);
	function serverpromotionproductseckillcontentctrl($rootScope,$stateParams,$http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder,Upload,$timeout,GoodsTypeService) {

		var vm = this;
		vm.message = '';
		vm.someClickHandler = someClickHandler;
		vm.selected = {};
		vm.selectAll = false;
		vm.toggleAll = toggleAll;
		vm.toggleOne = toggleOne;
		vm.dtInstance = {};
		var titleHtml = '<input type="checkbox" ng-model="gt.selectAll" ng-click="gt.toggleAll(gt.selectAll,gt.selected)">';
		vm.dtOptions = DTOptionsBuilder.newOptions().withOption('ajax', {
				type: 'POST',
				url: '../mall/products/findProductByPage',
				dataSrc: 'data'
			})
			.withOption('processing', true)
			.withOption('paging', true)
			.withOption('serverSide', true)
			.withOption('createdRow', function (row, data, dataIndex) {
				$compile(angular.element(row).contents())($scope);
			})
			.withOption('headerCallback', function (header) {
				if (!$scope.headerCompiled) {
					$scope.headerCompiled = true;
					$compile(angular.element(header).contents())($scope);
				}
			})
			.withOption('stateSave', true)
			.withOption('rowCallback', rowCallback)
			.withPaginationType('full_numbers')
			.withLanguageSource('./app/language/chinese.json')

		$scope.$on('handleRequest', function () {

		});
		vm.dtColumns = [
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function (data, type, full, meta) {
				vm.selected[full.id] = false;
				return "<input type='checkbox' ng-model=\"gt.selected['" + data.id + "']\" ng-click=\"gt.toggleOne(gt.selected)\">";
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn(null).withTitle('图片').notSortable().renderWith(imgHtml),
			DTColumnBuilder.newColumn('psn').withTitle('货号').notSortable(),
			DTColumnBuilder.newColumn('productname').withTitle('货物名称').notSortable(),
			DTColumnBuilder.newColumn('nname').withTitle('分类').notSortable(),
			DTColumnBuilder.newColumn('goodstypename').withTitle('商品类型').notSortable(),
			DTColumnBuilder.newColumn('stock').withTitle('库存').notSortable(),
			DTColumnBuilder.newColumn('saleprice').withTitle('销售价').notSortable(),
			DTColumnBuilder.newColumn('memberprice').withTitle('会员价').notSortable(),
			DTColumnBuilder.newColumn('salestatus').withTitle('销售状态').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable()];
		function imgHtml(data, type, full, meta) {
			return '<img style="width:50px;height:50px;" ng-src="' + data.mainimg + '" alt="Photo" />';
		}

		function reloadData() {
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}

		function callback(data) {
		}


		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData() {
			var resetPaging = false;
			var searchParams = {
				isCustomSearch: 1,
				name: '',
				psn: '',
				goodstypeid: ''
			};
			if ($scope.productname != undefined) {
				searchParams.name = $scope.productname;

			}
			if ($scope.psn != undefined) {
				searchParams.psn = $scope.psn;
			}
			if ($scope.selectedgoodstype.goodstypeid != undefined) {
				searchParams.goodstypeid = $scope.selectedgoodstype.goodstypeid;
			} else {
				$scope.errors = {
					show: true,
					msg: '请选择商品类型'
				}
				return false;
			}
			vm.dtOptions['ajax'].data = searchParams;
			vm.dtInstance.reloadData(callback, resetPaging);
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom = function () {
			searchReloadData();
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll, selectedItems) {
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					selectedItems[id] = selectAll;
				}
			}
		}

		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems) {
			var me = this;
			for (var id in selectedItems) {
				if (selectedItems.hasOwnProperty(id)) {
					if (!selectedItems[id]) {
						me.selectAll = false;
					}
				}
			}
			me.selectAll = true;

		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
			$scope.pushToSelectedProducts(info.id, info);
		}

		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function () {
				$scope.$apply(function () {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}


		//选定的专题货物

		$scope.pushToSelectedProducts = function (index, product) {
			//var tag=true;
			//angular.forEach($scope.sp,function(d){
			//	if(d.id==product.id){
			//		alert("该商品已经存在");
			//		tag=false;
			//	}
			//});
			//if(tag){
			//	$scope.sp.push(product);
			//}
			$scope.sp=[];
			$scope.sp.push(product);
			$scope.selectedproducts=$scope.sp;
		};


		/**
		 * 删除选择的货物
		 * @param index
		 */
		$scope.removeselectproduct = function (index) {
			$scope.selectedproducts.splice(index, 1);
			$scope.sp.slice(index,1);
		}


		//查询所有商品类型
		$scope.findallgoodstype = function () {
			var promise = GoodsTypeService.query();
			promise.then(function (data) {
				$scope.goodstypeselect = data.beanlist;
			}, function (data) {
				$scope.errors = {
					show: true,
					msg: '系统异常'
				}
			});
		};


		/**
		 * 显示货物搜索区域
		 */
		$scope.showsearchproduct = function () {
			$scope.searchproduct = true;
		}



		$scope.$watch('rmainfiles', function () {
			$scope.uploadrmainfiles($scope.rmainfiles);
		});
		$scope.allrmainfiles = [];
		$scope.uploadrmainfiles = function (files) {
			if (files && files.length) {
				for (var i = 0; i < files.length; i++) {
					var file = files[i];
					var filename = files[i].name;
					if (!file.$error) {
						Upload.upload({
							url: '../mall/files/ajaxFileUploadsImgs',
							data: {
								file: file,
								filename: filename
							}
						}).progress(function (evt) {
							var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
							$scope.rmainprogress = progressPercentage;
						}).success(function (data, status, headers, config) {
							$timeout(function () {
								if (data.ifb.sucflag) {
									$scope.allrmainfiles.push(data.ifb);
								}
							});
						});
					}
				}
			}
		}

		$scope.removeallrmainfiles = function (index) {
			$scope.allrmainfiles.splice(index, 1);
		}


		var operate = $location.search().operate || $stateParams.operate;
		if (operate != undefined && operate == "save") {
			$scope.title = "添加秒杀商品信息";
			$scope.promotionProductSeckillId=$location.search().promotionProductSeckillId;
			//控制保存按钮显示
			$scope.savebtn = {show: true};
			//设置启用 1表示启用
			$scope.status = "1";

			$scope.save = function () {
				if ($scope.producttitle == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品标题'
					}
					return false;
				}
				if ($scope.price == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入销售价格'
					}
					return false;
				}
				if ($scope.seckillprice == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀价格'
					}
					return false;
				}
				if ($scope.total == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀库存'
					}
					return false;
				}
				if ($scope.content == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品介绍'
					}
					return false;
				}
				if ($scope.des == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品清单说明'
					}
					return false;
				}
				if ($scope.allrmainfiles.length == 0) {
					$scope.errors = {
						show: true,
						msg: '请上传秒杀封面'
					}
					return false;
				}
				if($scope.selectedproducts.length==0){
					$scope.errors = {
						show: true,
						msg: '请选择商品'
					}
					return false;
				}

				$http({
					method: 'POST',
					url: '../mall/promotion/productseckillcontent/save',
					data: {
						'producttitle': $scope.producttitle,
						'price':$scope.price,
						'seckillprice':$scope.seckillprice,
						'mainimg': angular.toJson($scope.allrmainfiles),
						'productjson':angular.toJson($scope.selectedproducts[0]),
						'total':$scope.total,
						'content':$scope.content,
						'promotionProductSeckillId':$scope.promotionProductSeckillId,
						'des': $scope.des,
						'productid':$scope.selectedproducts[0].id,
						'status': $scope.status
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.info = {
							show: true,
							msg: '秒杀商品添加成功'
						}
						$location.path('/promotionproductseckillcontentment');
					} else {
						$scope.errors = {
							show: true,
							msg: data.msg
						}
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}
		}

		if (operate == 'edit') {
			var id = $location.search().id;
			if (id != undefined && id != "") {
				$http({
					method: 'POST',
					url: '../mall/promotion/productseckillcontent/find',
					data: {
						'id': id
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.title = '正在对秒杀商品--' + data.bean.sectiontitle + '--进行编辑';
						$scope.producttitle = data.bean.producttitle;
						$scope.price=data.bean.price;
						$scope.seckillprice=data.bean.seckillprice;
						$scope.rquantitly=data.bean.rquantitly;
						$scope.total=data.bean.total;
						$scope.des = data.bean.des;
						$scope.content=data.bean.content;
						$scope.status = data.bean.status;
						$scope.allrmainfiles.push(data.ifb);
						$scope.selectedproducts = data.beanlist;
						$scope.readonlytotal=true;
						$scope.readonlyrquantity=true;
						$scope.updatebtn = {show: true};
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

			$scope.update = function () {
				if ($scope.producttitle == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品标题'
					}
					return false;
				}
				if ($scope.price == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入销售价格'
					}
					return false;
				}
				if ($scope.seckillprice == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀价格'
					}
					return false;
				}
				if ($scope.total == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀库存'
					}
					return false;
				}
				if ($scope.content == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品介绍'
					}
					return false;
				}
				if ($scope.des == undefined) {
					$scope.errors = {
						show: true,
						msg: '请输入秒杀商品清单说明'
					}
					return false;
				}
				if ($scope.allrmainfiles.length == 0) {
					$scope.errors = {
						show: true,
						msg: '请上传秒杀封面'
					}
					return false;
				}
				if($scope.selectedproducts.length==0){
					$scope.errors = {
						show: true,
						msg: '请选择商品'
					}
					return false;
				}
				$http({
					method: 'POST',
					url: '../mall/promotion/productseckillcontent/update',
					data: {
						'id': id,
						'producttitle': $scope.producttitle,
						'price':$scope.price,
						'seckillprice':$scope.seckillprice,
						'mainimg': angular.toJson($scope.allrmainfiles),
						'productjson':angular.toJson($scope.selectedproducts[0]),
						'total':$scope.total,
						'content':$scope.content,
						'promotionProductSeckillId':$scope.promotionProductSeckillId,
						'des': $scope.des,
						'productid':$scope.selectedproducts[0].id,
						'status': $scope.status
					}
				}).success(function (data, status, headers, config) {
					if (data.sucflag) {
						$scope.info = {
							show: true,
							msg: '秒杀商品更新成功'
						}
						$location.path('/promotionproductseckillcontentment').search({'operate':'edit','seckilltitle':$rootScope.gloseckilltitle,'id':$rootScope.glopromotionProductSeckillId});

				} else {
						$scope.errors = {
							show: true,
							msg: data.msg
						}
					}
				}).error(function (data, status, headers, config) {
					$scope.errors = {
						show: true,
						msg: '系统异常'
					}
				});
			}

		}
	}



	module.controller('promotionproductseckillcontentment',serverpromotionproductseckillmentctrl);
	//查询列表数据
	function serverpromotionproductseckillmentctrl($rootScope,$http,$location,$compile,$scope,DTOptionsBuilder,DTColumnBuilder){
		$scope.seckilltitle=$location.search().seckilltitle;
		$scope.promotionProductSeckillId=$location.search().id;
		$rootScope.gloseckilltitle=$scope.seckilltitle;
		$rootScope.glopromotionProductSeckillId=$scope.promotionProductSeckillId;
		$scope.title='正在查看秒杀--'+$scope.seckilltitle+'--的内容列表';
		var vm=this;
		vm.message='';
		vm.someClickHandler = someClickHandler;
		vm.selected={};
		vm.selectAll=false;
		vm.toggleAll=toggleAll;
		vm.toggleOne=toggleOne;
		vm.dtInstance={};
		var titleHtml='<input type="checkbox" ng-model="showCase.selectAll" ng-click="showCase.toggleAll(showCase.selectAll,showCase.selected)">';
		vm.dtOptions=
			DTOptionsBuilder.newOptions().withOption('ajax',{
					type:'POST',
					url:'../mall/promotion/productseckillcontent/findPskContentBySeckillIdByPage',
					data:{
						promotionProductSeckillId:$scope.promotionProductSeckillId
					},
					dataSrc:'data'
				})
				.withOption('processing',false)
				.withOption('paging',true)
				.withOption('serverSide',true)
				.withOption('createdRow',function(row,data,dataIndex){
					$compile(angular.element(row).contents())($scope);
				})
				.withOption('headerCallback', function(header) {
					if (!$scope.headerCompiled) {
						$scope.headerCompiled = true;
						$compile(angular.element(header).contents())($scope);
					}
				})
				.withOption('stateSave', true)
				.withOption('rowCallback',rowCallback)
				.withPaginationType('full_numbers')
				.withLanguageSource('./app/language/chinese.json')
		$scope.$on('handleRequest',function(){

		});
		vm.dtColumns=[
			DTColumnBuilder.newColumn(null).withTitle(titleHtml).notSortable().renderWith(function(data,type,full,meta){
				vm.selected[full.id]=false;
				return '<input type="checkbox" ng-model="showCase.selected['+data.id+']" ng-click="showCase.toggleOne(showCase.selected)">';
			}),
			DTColumnBuilder.newColumn('id').withTitle('ID').notVisible(),
			DTColumnBuilder.newColumn(null).withTitle('图标').notSortable().renderWith(imgHtml),
			DTColumnBuilder.newColumn('producttitle').withTitle('秒杀商品标题').notSortable(),
			DTColumnBuilder.newColumn('price').withTitle('销售价格').notSortable(),
			DTColumnBuilder.newColumn('seckillprice').withTitle('秒杀价格').notSortable(),
			DTColumnBuilder.newColumn('total').withTitle('秒杀库存').notSortable(),
			DTColumnBuilder.newColumn('rquantity').withTitle('剩余秒杀库存').notSortable(),
			DTColumnBuilder.newColumn('productid').withTitle('秒杀货物ID').notSortable(),
			DTColumnBuilder.newColumn('status').withTitle('状态').notSortable(),
			DTColumnBuilder.newColumn('updatetime').withTitle('更新时间').notSortable(),
			DTColumnBuilder.newColumn('version').withTitle('版本号').notSortable(),
			DTColumnBuilder.newColumn(null).withTitle('操作').notSortable().renderWith(actionHtml)];

		function imgHtml(data,type,full,meta){
			return '<img style="width:50px;height:50px;" ng-src="'+data.smainimg+'" alt="Photo" />';
		}
		function actionHtml(data,type,full,meta){
			return '<button class="btn btn-warning" ng-click="edit('+data.id+')"><i class="fa fa-edit"></i></button>';
		}

		function reloadData(){
			var resetPaging = false;
			vm.dtInstance.reloadData(callback, resetPaging);
		}
		function callback(data){
		}


		/**
		 * 复杂搜索刷新数据
		 */
		function searchReloadData(){
			var resetPaging=false;
			var searchParams={
				isCustomSearch:1,
				name:''
			};
			if($scope.name!=undefined){
				searchParams.name=$scope.name;
				vm.dtOptions['ajax'].data=searchParams;
				vm.dtInstance.reloadData(callback, resetPaging);
			}
		}

		/**
		 * 表格
		 */
		$scope.searchdataTableCustom=function(){
			searchReloadData();
		}


		//表格中编辑按钮
		$scope.edit=function(id,name){
			$location.path('/promotionproductseckillcontent').search({'operate':'edit','id':id});
		}


		/**
		 * 添加秒杀内容
		 */
		$scope.save=function(){
			$location.path('/promotionproductseckillcontent').search({'operate':'save','seckilltitle':$scope.seckilltitle,'promotionProductSeckillId':$scope.promotionProductSeckillId});
		}

		$scope.del=function(){
			var i=0;
			var ids=[];
			angular.forEach(vm.selected, function(data,index,array){
				if(data){
					i++;
					ids.push(index);
				}
			});
			if(i==0){
				$scope.errors={
					show:true,
					msg:'请选择一条记录'
				}
			}else{
				$scope.errors={
					show:false
				}
				//批量删除数据
				var idstrs=ids.join(",");
				$http({
					method:'POST',
					url:'../mall/promotion/productseckillcontent/del',
					data:{
						'id':idstrs
					}
				}).
				success(function(data,status,headers,config){
					if(data.sucflag){
						$scope.info={
							show:true,
							msg:'删除成功'
						}
						reloadData();
					}

				}).
				error(function(data,status,headers,config){
					$scope.errors={
						show:true,
						msg:'系统异常'
					}
				});
			}
		}

		/**
		 * 列表全选
		 */
		function toggleAll(selectAll,selectedItems){
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					selectedItems[id]=selectAll;
				}
			}
		}
		/**
		 * 列表单选
		 */
		function toggleOne(selectedItems){
			var me=this;
			for(var id in selectedItems){
				if(selectedItems.hasOwnProperty(id)){
					if(!selectedItems[id]){
						me.selectAll=false;
					}
				}
			}
			me.selectAll=true;
		}

		function someClickHandler(info) {
			vm.message = info.id + ' - ' + info.name;
		}
		/**
		 * 单击列表某行回调
		 */
		function rowCallback(nRow,aData,iDisplayIndex,iDisplayIndexFull){
			$('td', nRow).unbind('click');
			$('td', nRow).bind('click', function() {
				$scope.$apply(function() {
					vm.someClickHandler(aData);
				});
			});
			return nRow;
		}



	}
});
